﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("soupTask")]
    public class SoupTaskController : AdminController
    {
        private ISoupTaskService _iSoupTaskService;

        public SoupTaskController(ISoupTaskService iSoupTaskService)
        {
            _iSoupTaskService = iSoupTaskService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult Gets(SoupTaskQuery query)
        {
            var model = _iSoupTaskService.GetSoupTasks(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get(long id)
        {
            soup_task_dto_ex model = new soup_task_dto_ex() { type = ETaskType.Register, category = ESoupTaskCategory.Based, apply = ESoupTaskApply.APP, status = ESoupTaskStatus.Normal };//新增时也要获得任务列表下拉数据
            if (id > 0)
                _iSoupTaskService.GetSoupTask(id).ToCopyProperty(model);
            model.type_value = ((int)model.type).ToString();
            model.category_value = ((int)model.category).ToString();
            model.apply_value = ((int)model.apply).ToString();
            model.status_value = ((int)model.status).ToString();
            model.logo_url = model.logo_url ?? "";
            model.sub_title = model.sub_title ?? "";
            model.url = model.url ?? "";
            model.prompt = model.prompt ?? "";
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Add(soup_task_dto_ex dto)
        {
            dto.type = (ETaskType)int.Parse(dto.type_value);
            dto.category = (ESoupTaskCategory)int.Parse(dto.category_value);
            dto.apply = (ESoupTaskApply)int.Parse(dto.apply_value);
            dto.status = (ESoupTaskStatus)int.Parse(dto.status_value);
            var file_url = Upload("SoupTask");
            if (!string.IsNullOrEmpty(file_url))
                dto.logo_url = file_url;

            var result = _iSoupTaskService.AddSoupTask(dto);
            return Json(result, JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Update(soup_task_dto_ex dto)
        {
            dto.type = (ETaskType)int.Parse(dto.type_value);
            dto.category = (ESoupTaskCategory)int.Parse(dto.category_value);
            dto.apply = (ESoupTaskApply)int.Parse(dto.apply_value);
            dto.status = (ESoupTaskStatus)int.Parse(dto.status_value);
            var file_url = Upload("SoupTask");
            if (!string.IsNullOrEmpty(file_url))
                dto.logo_url = file_url;

            var result = _iSoupTaskService.UpdateSoupTask(dto);
            return Json(result, JsonRequestBehavior.AllowGet);

        }

        [HttpDelete]
        public JsonResult Delete(long id)
        {
            var result = _iSoupTaskService.DeleteSoupTask(id);
            return Json(ResultToJson.Json(result > 0 ? " 删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult UpdateStatus(long id, int status)
        {
            var result = _iSoupTaskService.UpdateSoupTaskStatus(id, status);
            return Json(ResultToJson.Json(result > 0 ? " 修改成功" : "修改失败", result));
        }

        public JsonResult GetUserSoupValueHiss(UserSoupValueHisQuery query)
        {
            var model = _iSoupTaskService.GetUserSoupValueHiss(query);
            return Json(ResultToJson.Json(new { e_task_type_list = typeof(ETaskType).GetDropdownItemList(), data =model }), JsonRequestBehavior.AllowGet);
        }

    }
}